package com.dcy.znoa.service.publiz.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dcy.znoa.common.util.pojo.JsonCommon;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.publiz.model.SelectBaseModel;
import com.dcy.znoa.controller.publiz.model.news.SelectNewsModel;
import com.dcy.znoa.dao.publiz.CompanyDao;
import com.dcy.znoa.dao.publiz.NewsDao;
import com.dcy.znoa.domain.publiz.Company;
import com.dcy.znoa.domain.publiz.News;
import com.dcy.znoa.service.publiz.NewsService;
import com.dcy.znoa.service.sys.StoreFileService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

@Service
public class NewsServiceImpl implements NewsService {

    @Autowired
    NewsDao newsDao;
    @Autowired
    CompanyDao companyDao;
    @Autowired
    StoreFileService storeFileService;

    @Override
    public JsonForList selectList(SelectNewsModel model) throws InvocationTargetException, IllegalAccessException {
//        News cp = new News();
//        BeanUtils.copyProperties(cp, model);
        Page page = new Page(model.getPageNum(), model.getPageSize());
        List<News> list = newsDao.selectListMappper(model, page);
        for (News news:list) {
            news.setStoreFileList(storeFileService.selectStoreFileByEntityId(news.getNewsId(),"news",null));
        }
        return new JsonForList(list,page.getTotal());
    }

    @Override
    public JsonCommon insert(News var1) {
        Integer result = newsDao.insert(var1);
        if (result == null || result <= 0) {
            return new JsonCommon(JsonCommon.INSERT_ERROR_CODE);
        }
        return new JsonCommon(JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
    }

    @Override
    public JsonCommon updateById(News var1) {
        Integer result = newsDao.updateById(var1);
        if (result == null || result <= 0) {
            return new JsonCommon(JsonCommon.INSERT_ERROR_CODE);
        }
        return new JsonCommon(JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
    }
}
